package com.szsfm.vehiclemanagement.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.orhanobut.logger.Logger;
import com.szsfm.vehiclemanagement.R;
import com.szsfm.vehiclemanagement.base.BaseActivity;
import com.szsfm.vehiclemanagement.bean.Latlng;
import com.szsfm.vehiclemanagement.bean.User;
import com.szsfm.vehiclemanagement.event.CommEvent;
import com.szsfm.vehiclemanagement.event.LoginResultEvent;
import com.szsfm.vehiclemanagement.listener.LocationListener;
import com.szsfm.vehiclemanagement.mvp.presenter.LocationPresenter;
import com.szsfm.vehiclemanagement.mvp.presenter.LoginPresenter;
import com.szsfm.vehiclemanagement.mvp.view.ILocationView;
import com.szsfm.vehiclemanagement.mvp.view.IUserLoginView;
import com.szsfm.vehiclemanagement.tcp.TcpCore;
import com.szsfm.vehiclemanagement.tcp.TcpUtil;
import com.szsfm.vehiclemanagement.util.DeviceUtil;
import com.szsfm.vehiclemanagement.util.PermissionsChecker;
import com.szsfm.vehiclemanagement.util.ToastUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;


public class LoginActivity extends BaseActivity implements View.OnClickListener, LocationListener, IUserLoginView, ILocationView {

    private Button mLoginButton;
    private PermissionsChecker mPermissionsChecker;
    private EditText mUsernameEditText;
    private EditText mPasswordEditText;


    private LoginPresenter loginPresenter;
    private LocationPresenter locationPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        Logger.i("屏幕宽=" + DeviceUtil.getScreenWidth(this) + " 高=" + DeviceUtil.getScreenHeight(this));
        Logger.i("产品:" + DeviceUtil.getProductName());
        if (User.shareInstance().getUserID()!=null){
            MainActivity.startSelf(this);
            return;
        }
        mPermissionsChecker = new PermissionsChecker(this);
        mLoginButton = findViewById(R.id.login_button);
        mLoginButton.setOnClickListener(this);
        mUsernameEditText = findViewById(R.id.username_edt);
        mPasswordEditText = findViewById(R.id.password_edt);

        loginPresenter = new LoginPresenter(this);
        loginPresenter.setContext(getApplicationContext());
        loginPresenter.prepare();//做初始化工作，比如设置edittext默认用户名和密码
        locationPresenter = new LocationPresenter(this, getApplicationContext());
        locationPresenter.startLocation();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onLoginEvent(LoginResultEvent ret){
        Logger.d("登录:"+(ret.ret.equals("0")?"成功":"失败"+ret.ret));
        if (ret.ret.equals("0")){
            loginPresenter.loginSuccess(User.shareInstance());
            TcpCore.queryParam(User.shareInstance().getUserID());
        }else {
            ToastUtil.show(""+ret.msg);
        }

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onGetParam(CommEvent ev){
        if (ev.type==CommEvent.EVENT_TYPE_PARAM){
            if (ev.ret==0){
                MainActivity.startSelf(this);
            }
        }else if(ev.type==CommEvent.EVENT_TYPE_SO_TIME_OUT){
            TcpCore.enableConn = false;
            TcpUtil.stopSavService(this);
        }
    }

    @Override
    public void onReceiveLocation(double lat, double lng, String place) {
        Logger.i("定位地址:" + place);
    }

    @Override
    protected void onResume() {
        super.onResume();
        // 缺少权限时, 进入权限配置页面
        if (mPermissionsChecker.lacksPermissions(PermissionsChecker.PERMISSIONS)) {
            startPermissionsActivity();
            return;
        }

    }

    private void startPermissionsActivity() {
        PermissionsActivity.startActivityForResult(this, PermissionsChecker.REQUEST_CODE, PermissionsChecker.PERMISSIONS);
    }


    @Override
    public void onClick(View view) {
        String usernameText = mUsernameEditText.getText().toString().trim();
        String passwordText = mPasswordEditText.getText().toString().trim();
        if(User.shareInstance().getUserID()!=null&&TcpCore.isruning()){
           MainActivity.startSelf(this);
        }else {
            loginPresenter.login(usernameText, passwordText);
        }
    }


    @Override
    public void startLogin() {
        ToastUtil.show("正在登陆");
    }

    @Override
    public void loginError(String s) {
        ToastUtil.show(s);
    }

    @Override
    public void showDefaultUser(String username, String password) {
        mUsernameEditText.setText(username);
        mPasswordEditText.setText(password);
    }

    @Override
    public void startLocation() {
        //ToastUtil.show("正在定位");
    }

    @Override
    public void locationResult(Latlng latlng) {
       // ToastUtil.show("定位成功:" + latlng.getLatitue() + "," + latlng.getLongitue());
    }

    @Override
    protected void onStop() {
        super.onStop();
        Logger.d("onStop");
        EventBus.getDefault().unregister(this);
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (!EventBus.getDefault().isRegistered(this))
        EventBus.getDefault().register(this);
    }
}

